<template>
  <div class="poem-container myCenter my-animation-hideToShow">
    <!-- 背景图片 -->
    <var-image lazy   fit="cover" class="my-el-image poem-image" :src="two_poem_image[Math.floor(Math.random() * two_poem_image.length)]">
      <div slot="error" class="image-slot"></div>
    </var-image>

    <div class="poem-wrap">
      <div v-if="isShehui"><span>鬼畜全明星</span></div>
      <div v-else><span>{{ isHitokoto ? hitokoto.from : guShi.origin }}</span></div>
      <p class="poem">{{ isHitokoto ? hitokoto.hitokoto : guShi.content }}</p>
      <p class="info" v-if="!isShehui && (!isHitokoto || (isHitokoto && !$common.isEmpty(hitokoto.from_who)))">
        {{ isHitokoto ? hitokoto.from_who : guShi.author }}
      </p>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, onMounted } from 'vue'
import axios from "axios"
import constant from '@/utils/constant'
export default {
  name: '',
  setup() {
    const data = reactive({
      two_poem_image: ["https://s1.ax1x.com/2022/12/04/zsKgDs.jpg",
        "https://s1.ax1x.com/2022/12/04/zsKh5V.jpg"],
      guShi: {
        "content": "...",
        "origin": "...",
        "author": "...",
        "category": "..."
      },
      hitokoto: {
        "hitokoto": "...",
        "from": "...",
        "from_who": "..."
      }
    })
    const sendShehui = () => {
      axios.get(constant.shehui).then(res => {
        data.hitokoto = shehui.substring(1, shehui.length - 1);
      }).catch(err => {
     
      })
    }
    const getGuShi = () => {
      axios.get(constant.jinrishici).then(res => {
        data.guShi = res.data;
      }).catch(err => {
  
      })
    }
    const getHitokoto = () => {
      axios.get(constant.hitokoto).then(res => {
        data.hitokoto = JSON.parse(res.data);
      }).catch(err => {

      })
    }
    onMounted(() => {
      if (!data.isShehui) {
        if (data.isHitokoto) {
          getHitokoto();
        } else {
          getGuShi();
        }
      } else {
        data.hitokoto.from = "";
        data.hitokoto.from_who = "";
        sendShehui();
      }
    })
    return {
      ...toRefs(data),
    }
  },
}

</script>
<style scoped lang='scss'>
.poem-container {
  width: 100%;
  height: 50vh;
  padding: 0 0 40px;
  position: relative;
}

.poem-wrap {
  position: absolute;
  border-radius: 10px;
  z-index: 10;
  text-align: center;
  letter-spacing: 4px;
  font-weight: 300;
  width: 100%;
  max-width: 800px;
}

.poem-wrap div span {
  padding: 5px 10px;
  color: var(--white);
  font-size: 2em;
  border-radius: 5px;
}

.poem-wrap p {
  width: 100%;
  max-width: 800px;
  color: var(--white);
}

.poem-wrap p.poem {
  margin: 40px auto;
  font-size: 1.5em;
}

.poem-wrap p.info {
  margin: 20px auto 40px;
  font-size: 1.1em;
}
.poem-image::before {
  height: calc(100% - 40px);
}
</style>